package com.otdemo.dao;

import com.mysql.cj.util.StringUtils;
import com.otdemo.entity.RestClassification;
import com.otdemo.util.DBUtils;
import com.sun.javaws.CacheUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RestClassificationDao {

    public void insert(RestClassification classification) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "insert into stu_classification\n" +
                    "(uid, name, deleted, createDate, updateDate,operator) \n" +
                    "values (?, ?, 0, now(), now(), ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, UUID.randomUUID().toString());
            statement.setString(2, classification.getName());
            statement.setString(3, classification.getOperator());
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void update(RestClassification classification) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "update stu_classification set \n" +
                    "name       = ?,\n" +
                    "deleted    = ?,\n" +
                    "updateDate = now(),\n" +
                    "updateDate = now() \n" +
                    "where uid   = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, classification.getName());
            statement.setBoolean(2, classification.getDeleted());
            statement.setString(3, classification.getUid());
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public RestClassification findByUid(String queryUid) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "select id, uid, name, deleted, createDate, updateDate, operator " +
                    "from stu_classification where uid = ? and deleted = 0 ";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, queryUid);
            ResultSet rs = statement.executeQuery();
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestClassification classification = new RestClassification();
                classification.setId(id);
                classification.setUid(uid);
                classification.setName(name);
                classification.setDeleted(deleted);
                classification.setCreateDate(createDate);
                classification.setUpdateDate(updateDate);
                classification.setOperator(operator);
                return classification;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public List<RestClassification> list() {
        List<RestClassification> classificationList = new ArrayList<>();
        ResultSet rs;
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "select id, uid, name, deleted, createDate, updateDate, operator " +
                    "from stu_classification where deleted = 0 ";
            PreparedStatement statement = connection.prepareStatement(sql);
            rs = statement.executeQuery();
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestClassification classification = new RestClassification();
                classification.setId(id);
                classification.setUid(uid);
                classification.setName(name);
                classification.setDeleted(deleted);
                classification.setCreateDate(createDate);
                classification.setUpdateDate(updateDate);
                classification.setOperator(operator);
                classificationList.add(classification);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return classificationList;
    }

}
